home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dmake / save / cmdlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-19  |  7.6 KB  |  376 lines

  1.  
  2. /*
  3.  *  CMDLIST.c
  4.  *
  5.  */
  6.  
  7. #include "defs.h"
  8.  
  9. Prototype void InitCmdList(void);
  10. Prototype void PutCmdListChar(List *, char);
  11. Prototype void PutCmdListSym(List *, char *, short *);
  12. Prototype void CopyCmdList(List *, List *);
  13. Prototype void AppendCmdList(List *, List *);
  14. Prototype int  PopCmdListSym(List *, char *, long);
  15. Prototype int  PopCmdListChar(List *);
  16. Prototype void CopyCmdListBuf(List *, char *);
  17. Prototype void CopyCmdListNewLineBuf(List *, char *);
  18. Prototype long CmdListSize(List *);
  19. Prototype long CmdListSizeNewLine(List *);
  20. Prototype void CopyCmdListConvert(List *, List *, char *, char *);
  21. Prototype long ExecuteCmdList(DepNode *, List *);
  22.  
  23. List CmdFreeList;
  24. __aligned char CmdTmp1[256];
  25. __aligned char CmdTmp2[256];
  26.  
  27. void
  28. InitCmdList()
  29. {
  30.     NewList(&CmdFreeList);
  31. }
  32.  
  33. void
  34. PutCmdListChar(List *list, char c)
  35. {
  36.     CmdNode *node;
  37.  
  38.     if ((node = GetTail(list)) == NULL || (node->cn_Idx == node->cn_Max)) {
  39.     if ((node = RemHead(&CmdFreeList)) == NULL) {
  40.         node = malloc(sizeof(CmdNode) + 64);
  41.         node->cn_Node.ln_Name = (char *)(node + 1);
  42.         node->cn_Max = 64;
  43.     }
  44.     node->cn_Node.ln_Type = 0;
  45.     node->cn_Idx = 0;
  46.     node->cn_RIndex = 0;
  47.     AddTail(list, &node->cn_Node);
  48.     }
  49.     node->cn_Node.ln_Name[node->cn_Idx++] = c;
  50. }
  51.  
  52. void
  53. PutCmdListSym(list, buf, pspace)
  54. List *list;
  55. char *buf;
  56. short *pspace;
  57. {
  58.     if (*buf) {
  59.     if (pspace) {
  60.         if (*pspace)
  61.         PutCmdListChar(list, ' ');
  62.         *pspace = 1;
  63.     }
  64.     /*if ((node = GetTail(list)) && node->cn_Idx && node->cn_Node.ln_Name[node->cn_Idx-1] != ' ')*/
  65.     while (*buf) {
  66.         PutCmdListChar(list, *buf);
  67.         ++buf;
  68.     }
  69.     }
  70. }
  71.  
  72. void
  73. CopyCmdList(fromList, toList)
  74. List *fromList;
  75. List *toList;
  76. {
  77.     CmdNode *from;
  78.     long n;
  79.     long i;
  80.  
  81.     for (from = GetHead(fromList); from; from = GetSucc(&from->cn_Node)) {
  82.     CmdNode *copy = NULL;
  83.  
  84.     dbprintf(("COPYFROM %*.*s\n", from->cn_Idx, from->cn_Idx, from->cn_Node.ln_Name));
  85.  
  86.     for (n = 0; n < from->cn_Idx; ) {
  87.         if ((copy = RemHead(&CmdFreeList)) == NULL) {
  88.         copy = malloc(sizeof(CmdNode) + 64);
  89.         copy->cn_Max = 64;
  90.         copy->cn_Node.ln_Name = (char *)(copy + 1);
  91.         }
  92.         i = (copy->cn_Max < from->cn_Idx - n) ? copy->cn_Max : from->cn_Idx - n;
  93.         copy->cn_Node.ln_Type = 0;
  94.         copy->cn_Idx = i;
  95.         copy->cn_RIndex = 0;
  96.         movmem(from->cn_Node.ln_Name + n, copy->cn_Node.ln_Name, i);
  97.         AddTail(toList, ©->cn_Node);
  98.         n += i;
  99.     }
  100.     if (copy)
  101.         copy->cn_Node.ln_Type = from->cn_Node.ln_Type;
  102.     }
  103. }
  104.  
  105. void
  106. AppendCmdList(fromList, toList)
  107. List *fromList;
  108. List *toList;
  109. {
  110.     CmdNode *from;
  111.  
  112.     while (from = RemHead(fromList))
  113.     AddTail(toList, &from->cn_Node);
  114. }
  115.  
  116.  
  117. /*
  118.  *  pop a symbol (symbols are separated by white space)
  119.  */
  120.  
  121. int
  122. PopCmdListSym(cmdList, buf, max)
  123. List *cmdList;
  124. char *buf;
  125. long max;
  126. {
  127.     short c;
  128.     short i = 0;
  129.  
  130.     --max;
  131.  
  132.     while ((c = PopCmdListChar(cmdList)) == ' ' || c == '\t' || c == '\n')
  133.     ;
  134.     while (c != EOF && c != ' ' && c != '\t' && c != '\n' && i < max) {
  135.     buf[i++] = c;
  136.     c = PopCmdListChar(cmdList);
  137.     }
  138.     buf[i] = 0;
  139.     return((i) ? 0 : -1);
  140. }
  141.  
  142. int
  143. PopCmdListChar(cmdList)
  144. List *cmdList;
  145. {
  146.     CmdNode *node;
  147.     short c = EOF;
  148.  
  149.     while (node = GetHead(cmdList)) {
  150.     if (node->cn_RIndex != node->cn_Idx)
  151.         return((ubyte)node->cn_Node.ln_Name[node->cn_RIndex++]);
  152.     Remove((struct Node *)node);
  153.     AddTail(&CmdFreeList, &node->cn_Node);
  154.     }
  155.     return(c);
  156. }
  157.  
  158. void
  159. CopyCmdListBuf(list, buf)
  160. List *list;
  161. char *buf;
  162. {
  163.     CmdNode *node;
  164.     long n = 0;
  165.  
  166.     while (node = RemHead(list)) {
  167.     movmem(node->cn_Node.ln_Name + node->cn_RIndex, buf, node->cn_Idx - node->cn_RIndex);
  168.     buf += node->cn_Idx;
  169.     AddTail(&CmdFreeList, &node->cn_Node);
  170.     }
  171.     buf[0] = 0;
  172. }
  173.  
  174. void
  175. CopyCmdListNewLineBuf(list, buf)
  176. List *list;
  177. char *buf;
  178. {
  179.     CmdNode *node;
  180.     long i;
  181.  
  182.     while (node = RemHead(list)) {
  183.     for (i = node->cn_RIndex; i < node->cn_Idx && node->cn_Node.ln_Name[i] != '\n'; ++i)
  184.         *buf++ = node->cn_Node.ln_Name[i];
  185.     if (i != node->cn_Idx) {
  186.         node->cn_RIndex = i + 1;
  187.         AddHead(list, &node->cn_Node);
  188.         break;
  189.     }
  190.     AddTail(&CmdFreeList, &node->cn_Node);
  191.     }
  192.     *buf = 0;
  193. }
  194.  
  195. long
  196. CmdListSize(list)
  197. List *list;
  198. {
  199.     CmdNode *node;
  200.     long n = 0;
  201.  
  202.     for (node = GetHead(list); node; node = GetSucc(&node->cn_Node))
  203.     n += node->cn_Idx - node->cn_RIndex;
  204.     return(n);
  205. }
  206.  
  207. long
  208. CmdListSizeNewLine(list)
  209. List *list;
  210. {
  211.     CmdNode *node;
  212.     long n = 0;
  213.     long i;
  214.  
  215.     for (node = GetHead(list); node; node = GetSucc(&node->cn_Node)) {
  216.     for (i = node->cn_RIndex; i < node->cn_Idx && node->cn_Node.ln_Name[i] != '\n'; ++i)
  217.         ;
  218.     n += i - node->cn_RIndex;
  219.     if (i != node->cn_Idx)
  220.         break;
  221.     }
  222.     return(n);
  223. }
  224.  
  225. void
  226. CopyCmdListConvert(fromList, toList, srcMat, dstMat)
  227. List *fromList;
  228. List *toList;
  229. char *srcMat;
  230. char *dstMat;
  231. {
  232.     List tmpList;
  233.     short space = 0;
  234.  
  235.     dbprintf(("fromlist %08lx copyconvert '%s' -> '%s'\n", GetHead(fromList), srcMat, dstMat));
  236.     srcMat = ExpandVariable(srcMat, NULL);
  237.     dstMat = ExpandVariable(dstMat, NULL);
  238.  
  239.     NewList(&tmpList);
  240.     CopyCmdList(fromList, &tmpList);
  241.  
  242.     if (!*srcMat)
  243.     {
  244.        if (!GetHead(&tmpList))
  245.        {
  246.           PutCmdListSym(toList, dstMat, &space);
  247.           return;
  248.        }
  249.        /* We need to replace the src and destination with meaningful wildcards */
  250.        srcMat = dstMat = "*";
  251.     }
  252.  
  253.     /*
  254.      *    run each symbol through the conversion
  255.      */
  256.  
  257.     while (PopCmdListSym(&tmpList, CmdTmp1, sizeof(CmdTmp1)) == 0)
  258.     {
  259.     if (WildConvert(CmdTmp1, CmdTmp2, srcMat, dstMat) == 0)
  260.     {
  261.         PutCmdListSym(toList, CmdTmp2, &space);
  262.     }
  263.     }
  264. }
  265.  
  266. /*
  267.  *  The command list is executed by making a duplicate of it then reparsing
  268.  *  it resolving variable references
  269.  *
  270.  */
  271.  
  272. long
  273. ExecuteCmdList(dep, list)
  274. DepNode *dep;
  275. List *list;
  276. {
  277.     List tmpSrc;
  278.     List tmpDst;
  279.     short c;
  280.     long r = 0;
  281.  
  282.     NewList(&tmpSrc);
  283.     NewList(&tmpDst);
  284.     CopyCmdList(list, &tmpSrc);
  285.  
  286.     while ((c = PopCmdListChar(&tmpSrc)) != EOF) {
  287.     if (c == '$' || c == '%') {
  288.         short c0 = c;
  289.  
  290.         c = PopCmdListChar(&tmpSrc);
  291.         if (c == '(') {     /*  Variable Ref */
  292.         char *spec = AllocPathBuffer();
  293.  
  294.         /*
  295.          *  copy variable specification into a buffer then resolve
  296.          *  it to tmpDst
  297.          */
  298.         spec[0] = c0;
  299.         spec[1] = c;
  300.         for (c0 = 2; (c = PopCmdListChar(&tmpSrc)) != EOF && c != ')' && c0 < PBUFSIZE - 3; ++c0) {
  301.             if (c == '\"') {
  302.             spec[c0++] = c;
  303.             while ((c = PopCmdListChar(&tmpSrc)) != EOF && c != '\"' && c0 < PBUFSIZE - 3)
  304.                 spec[c0++] = c;
  305.             }
  306.             spec[c0] = c;
  307.         }
  308.         if (c != ')')
  309.             error(FATAL, "bad variable spec in command list for %s", dep->dn_Node.ln_Name);
  310.         spec[c0++] = c;
  311.         spec[c0] = 0;
  312.         ExpandVariable(spec, &tmpDst);
  313.         FreePathBuffer(spec);
  314.         continue;
  315.         }
  316.         if (c != c0)        /*  $$, %% escape   */
  317.         PutCmdListChar(&tmpDst, c0);
  318.     }
  319.     PutCmdListChar(&tmpDst, c);
  320.     }
  321.  
  322.     /*
  323.      *    pop into a command buffer for execution
  324.      */
  325.  
  326.     {
  327.     long n;
  328.  
  329.     while (r <= 5 && (n = CmdListSizeNewLine(&tmpDst))) {
  330.         short allocated;
  331.         short quiet = 0;
  332.         short ignore= 0;
  333.         char *cmd;
  334.  
  335.         if (n >= sizeof(CmdTmp1) - 2) {    /*  avoid malloc    */
  336.         allocated = 1;
  337.         cmd = (char *)malloc(n + 2);
  338.         } else {
  339.         allocated = 0;
  340.         cmd = CmdTmp1;
  341.         }
  342.         while ((c = PopCmdListChar(&tmpDst)) != EOF && (c == ' ' || c == '\t'))
  343.         --n;
  344.         if (c == '@') {
  345.         quiet = 1;
  346.         c = PopCmdListChar(&tmpDst);
  347.         --n;
  348.         }
  349.         if (c == '-') {
  350.         ignore = 1;
  351.         c = PopCmdListChar(&tmpDst);
  352.         --n;
  353.         }
  354.         cmd[0] = c;
  355.         CopyCmdListNewLineBuf(&tmpDst, cmd + 1);
  356.  
  357.         if (c) {
  358.         cmd[n] = 0;
  359.  
  360.         if (quiet == 0)
  361.             printf("    %s\n", cmd);
  362.         if (NoRunOpt == 0 && cmd[0] != '#') {
  363.             r = Execute_Command(cmd, ignore);
  364.             if (r < 0)
  365.             r = 20;
  366.             if (ExitCode < r)
  367.             ExitCode = r;
  368.         }
  369.         }
  370.         if (allocated)
  371.         free(cmd);
  372.     }
  373.     }
  374.     return(r);
  375. }
  376.